#!/usr/local/bin/lua

function substring_count(s1, s2)
 local magic =  "[%^%$%(%)%%%.%[%]%*%+%-%?]"
 local percent = function(s)return "%"..s end
    return select( 2, s1:gsub( s2:gsub(magic,percent), "" ) )
end

-- Author: Rodrigo Azevedo
-- Description: Function to get an explosion by position of a delimiter.
function explodec (stringvar, delimiter, position)
  countchars = string.len(stringvar)
  countposition = 0
  countvectorvarposition = 0
  vectorvar = {}
  vectorvar[0] = ''
  vectorvar[1] = ''
  for i=0, countchars-1, 1 do
    if string.sub(stringvar, i+1, i+1) == delimiter then
      countposition = countposition + 1
      if countposition == position then
        countvectorvarposition = countvectorvarposition + 1
      else
        vectorvar[countvectorvarposition] = vectorvar[countvectorvarposition] .. string.sub(stringvar, i+1, i+1)
      end
    else
      vectorvar[countvectorvarposition] = vectorvar[countvectorvarposition] .. string.sub(stringvar, i+1, i+1)
    end
  end
  return vectorvar
end

function explodeoperator (stringvar, operator)
  countoperator = substring_count(stringvar, operator)
  for i=1, countoperator, 1 do
    e = explodec(stringvar, operator, i)
    if substring_count(e[0], '(') == substring_count(e[0], ')') and substring_count(e[1], '(') == substring_count(e[1], ')') then
     e[2] = operator
      return e
    end
  end
end

function countoperatoroutparen (stringvar, operator)
  countoperator = substring_count(stringvar, operator)
  countoperatorout = 0
  for i=1, countoperator, 1 do
    e = explodec(stringvar, operator, i)
    if substring_count(e[0], '(') == substring_count(e[0], ')') and substring_count(e[1], '(') == substring_count(e[1], ')') then
      countoperatorout = countoperatorout + 1
    end
  end
  return countoperatorout
end

function mathsolve(stringvar)
  -- Excluing all the spaces
  stringvar = string.gsub(stringvar, ' ', '')
  
  if countoperatoroutparen(stringvar, '+') ~= 0 then
    e = explodeoperator(stringvar, '+')
  elseif countoperatoroutparen(stringvar, '-') ~= 0 then
    e = explodeoperator(stringvar, '-')
  elseif countoperatoroutparen(stringvar, '*') ~= 0 then
    e = explodeoperator(stringvar, '*')
  elseif countoperatoroutparen(stringvar, '/') ~= 0 then
    e = explodeoperator(stringvar, '/')
  elseif countoperatoroutparen(stringvar, '^') ~= 0 then
    e = explodeoperator(stringvar, '^')
  -- Checa todas as funções matemáticas, caso seja uma inexistente retorna uma exceção, caso nenhuma delas, retorna a própria string
  else
    --return checkmathfunctionsinsolve(stringvar)
    return stringvar
  end
  
  if e[0] == '' then e[0] = '0' end
  if e[2] == '+' then return mathsolve(e[0]) + mathsolve(e[1])
  elseif e[2] == '-' then return mathsolve(e[0]) - mathsolve(e[1])
  elseif e[2] == '*' then return mathsolve(e[0]) * mathsolve(e[1])
  elseif e[2] == '/' then return mathsolve(e[0]) / mathsolve(e[1])
  elseif e[2] == '^' then return mathsolve(e[0]) ^ mathsolve(e[1])
  end
end

--a = explodeoperator('2*(3+5)+3', '+')
--print(a[1])
--print(a[1])
print(countoperatoroutparen('3*(3+5)', '+'))
--print(substring_count('oii', 'i'))
print(mathsolve('3*(3+5)'))